<?php

namespace App\Console\Commands\PolicyUpload;
use App\Repositories\Contracts\PlatformInterface;
use App\Repositories\Contracts\PolicyInterface;
use App\Services\Rabbitmq\RabbitmqServer;
use Illuminate\Console\Command;

class AppDataPullRabbitmqCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'appDataPull:hu';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    public static $uploadTime = 0;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    private $rabbitmqService = null;
    public function __construct()
    {
        parent::__construct();


    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(PolicyInterface $policyInterface)
    {
        //define('AMQP_DEBUG', true);
         $policyUploadService = new PolicyUploadService();

        /*$message = '{"adultFuelTax":20,"airportTax":50,"arrCode":"URC","arrDateTime":"2025-07-09 02:15:00","cabinInfoList":[{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"基础经济舱:ivayd:tlqgb","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2270,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"经济舱*100里程加享:ivayd:7f0mt","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2320,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"有礼经济舱*赠机长熊:ivayd:nw72t","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2360,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"经济舱*300里程加享:ivayd:o8j6e","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2370,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"有礼经济舱*赠中机模:ivayd:bi3tr","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2400,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"经济舱*500里程加享:ivayd:kt6ev","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2420,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"经济舱*800里程加享:ivayd:yd8cy","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2470,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"有礼经济舱*赠大机模:ivayd:roa82","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2550,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"经济舱*1200里程加享:ivayd:yyh1d","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2570,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Q","cabinName":"经济舱*2000里程加享:ivayd:zjo7n","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":2770,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"G","cabinName":"高级经济舱:ivayd:2h1k5","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":3200,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"Y","cabinName":"全价经济舱:ivayd:7q7lq","cabinNum":"10","checkinLuggage":20,"childPrice":0,"discountedPrice":0,"facePrice":3200,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李20KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"R","cabinName":"超值公务舱:ivayd:bbnm0","cabinNum":"1","checkinLuggage":30,"childPrice":0,"discountedPrice":0,"facePrice":3520,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李30KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"C","cabinName":"折扣公务舱:ivayd:cq2he","cabinNum":"1","checkinLuggage":30,"childPrice":0,"discountedPrice":0,"facePrice":4800,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李30KG","maxPassengerNum":0,"minPassengerNum":0},{"activityFacePrice":0,"activityLabel":"null","cabinCode":"J","cabinName":"全价公务舱:ivayd:bshhw","cabinNum":"8","checkinLuggage":30,"childPrice":0,"discountedPrice":0,"facePrice":8000,"infantPrice":0,"limitConditions":"[]","luggage":"免费托运行李30KG","maxPassengerNum":0,"minPassengerNum":0}],"childFuelTax":0,"depCode":"TNA","depDate":"2025-07-08","depDateTime":"2025-07-08 21:55:00","flightNo":"SC8711","planeType":"2638"}';
        $flight = json_decode($message,true);
        if ($flight) {

            //获取所有规则
            $policy = $policyInterface->get(['status' => 1]);
            if ($policy->isNotEmpty()) {

                //转换数据
                $flight['hs_code'] = substr($flight['flightNo'], 0, 2);
                $flight['depart_city_code'] = $flight['depCode'];
                $flight['arrived_city_code'] = $flight['arrCode'];
                $flight['date'] = substr($flight['depDate'], 0, 10);
                $flight['flight_code'] = $flight['flightNo'];

                $flight['source_id'] = 8;
                $flight['cabins'] = [];
                foreach ($flight['cabinInfoList'] as $key => $value) {

                    $flight['cabins'][] = [
                        'cabin_code' => $value['cabinCode'],
                        'source_type' => $value['cabinName'],
                        'price' => $value['facePrice'],
                        'seat' => $value['cabinNum'],
                        'activity_label' => $value['activityLabel'],
                    ];
                }

                $res = $policyUploadService->uploadPolicy([$flight], $policy);
                $policyUploadService->otaUploadPolicy();
            }
        }

return ;*/
        while(true) {

            try {
                $rabbitmqService = new RabbitmqServer('hb_baseData_host');

                self::$uploadTime = time();

                try {
                    $rabbitmqService->pop('appDataPush', function ($message) use ($policyInterface, $policyUploadService) {

                        try {
                            if ($message->body) {

                                $flight = json_decode($message->body, true);

                                if ($flight) {

                                    //获取所有规则
                                    $policy = $policyInterface->get(['status' => 1]);

                                    if ($policy->isNotEmpty()) {

                                        //转换数据
                                        $flight['hs_code'] = substr($flight['flightNo'], 0, 2);
                                        $flight['depart_city_code'] = $flight['depCode'];
                                        $flight['arrived_city_code'] = $flight['arrCode'];
                                        $flight['date'] = substr($flight['depDate'], 0, 10);
                                        $flight['flight_code'] = $flight['flightNo'];

                                        $flight['source_id'] = 8;
                                        $flight['cabins'] = [];
                                        foreach ($flight['cabinInfoList'] as $key => $value) {

                                            $flight['cabins'][] = [
                                                'cabin_code' => $value['cabinCode'],
                                                'source_type' => $value['cabinName'],
                                                'price' => $value['facePrice'],
                                                'seat' => $value['cabinNum'],
                                                'activity_label' => !empty($value['activityLabel']) ? $value['activityLabel'] : null,
                                            ];
                                        }

                                        $res = $policyUploadService->uploadPolicy([$flight], $policy);

                                    }

                                }
                            }

                            if (time() - self::$uploadTime >= 5) {

                                $policyUploadService->otaUploadPolicy();

                                self::$uploadTime = time();
                            }

                        } catch (\Exception $exception) {

                            var_dump('APP消费Redis报错' . $exception->getMessage());
                            var_dump('APP消费内容' . $message);
                        }

                        return true;
                    });

                } catch (\Exception $exception) {
                    logger('APP MQ关闭连接2:'.$exception->getMessage());
                }
            } catch (\Exception $exception) {

                var_dump('APP连接Rabbit失败'.$exception->getMessage());
            }

        }
    }
}
